﻿using CloudDetector.Utils;

namespace CloudDetector.Algorithms
{
    /// <summary>
    /// Tasseled Cap (Bojtos sapka) transzformációt megvalósító osztály.
    /// </summary>
    public class TasseledCap
    {
        #region Fields

        //a tasseled cap matrixa
        static readonly float[,] Matrix = { {0.291F, 0.249F, 0.480F, 0.557F, 0.444F, 0.171F},
                                            {0.145F, 0.176F, 0.332F, 0.340F, -0.621F, -0.419F},
                                            {0.846F, -0.073F, -0.464F, -0.003F, -0.049F, 0.012F}};

        //a C konstans amit hozzá kell adni
        static readonly float[] C = { 10.37F, -3.383F, 0.788F };

        #endregion

        #region Contructor

        public TasseledCap()
        {

        }

        #endregion

        #region Member Methods

        /// <summary>
        /// Tasseled Cap transzformációt számoló metódus.
        /// </summary>
        /// <param name="Layer">Bemeneő kép rétegei.</param>
        /// <returns>Transzformált értékek multidimenziós mátrixa</returns>
        public float[, ,] Calc(ImageLayers Layer)
        {
            byte[, ,] Layers = Layer.layers;
            float[, ,] Result = new float[3, Layer.Width, Layer.Height];

            float Tmp;
            //oszlop
            for (int j = 0; j < Layer.Height; ++j)
            {
                //sor
                for (int i = 0; i < Layer.Width; ++i)
                {
                    //mátrixszorzas
                    for (int k = 0; k < 3; ++k)
                    {
                        Tmp = 0;
                        for (int l = 0; l < 6; ++l)
                        {
                            Tmp += Matrix[k, l] * Layers[l, i, j];
                        }
                        Result[k, i, j] = Tmp;// +C[k];
                    }

                }
            }
            return Result;
        }

        #endregion
    }
}
